package cn.lingyangwl.agile.msg.push.assembly;

import cn.lingyangwl.agile.model.*;
import cn.lingyangwl.agile.msg.push.model.entity.*;
import cn.lingyangwl.agile.msg.push.model.rqrs.notice.*;
import cn.lingyangwl.framework.tool.core.*;
import com.alibaba.fastjson2.JSON;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.util.Collections;
import java.util.List;

/**
 * 消息模板
 *
 * @author shenguangyang
 */
@Mapper(componentModel = "spring")
public interface MsgNoticeAssembly extends BaseAssembly {

  @Mappings({
      @Mapping(source = "msgFields", target = "msgFields", qualifiedByName = "msgFieldJsonStrToList"),
      @Mapping(source = "channelTypes", target = "channelTypes", qualifiedByName = JSON_STR_TO_SET),
  })
  MsgNoticeResp toMsgNoticeResp(MsgNotice source);

  @Mappings({
      @Mapping(source = "msgFields", target = "msgFields", qualifiedByName = "msgFieldListToJsonStr"),
      @Mapping(source = "channelTypes", target = "channelTypes", qualifiedByName = SET_TO_JSON_STR),
  })
  MsgNotice toMsgNotice(MsgNoticeSaveReq source);

  @Mappings({
      @Mapping(source = "msgFields", target = "msgFields", qualifiedByName = "msgFieldJsonStrToList"),
      @Mapping(source = "channelTypes", target = "channelTypes", qualifiedByName = JSON_STR_TO_SET),
  })
  SubscribeMsgNoticeResp toSubscribeMsgNoticeResp(MsgNotice source);

  @Named("msgFieldJsonStrToList")
  default List<MsgNotice.MsgField> msgFieldJsonStrToList(String value) {
    if (StringUtils.isEmpty(value)) {
      return Collections.emptyList();
    }
    return JSON.parseArray(value, MsgNotice.MsgField.class);
  }

  @Named("msgFieldListToJsonStr")
  default String msgFieldListToJsonStr(List<MsgNotice.MsgField> value) {
    return JSON.toJSONString(value);
  }
}
